package com.ls;

import com.ls.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname JiaNuoSms
 * @Description 佳诺短信平台
 * @Date 2019/11/19 18:56
 * @Created by pengyu
 */
public class JiaNuoSms {
    private static final Logger LOGGER = LoggerFactory.getLogger(JiaNuoSms.class);

    /**
     * 通用的。返回map格式 ,临时放在这个类里面
     * XML格式字符串转换为Map
     * @param strXml XML字符串
     * @return XML数据转换后的Map
     * @throws Exception
     */
    public static Map<String, String> xmlToMap(String strXml) throws Exception {
        try {
            Map<String, String> data = new HashMap(16);
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXml.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try {
                stream.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                LOGGER.error("xmlToMap error:"+ex.getMessage());
            }
            return data;
        } catch (Exception ex) {
            LOGGER.error("xmlToMap error:"+ex.getMessage());
            throw ex;
        }

    }
     /**
     * 企业id：1553
     * http://www.wp114.cn
     * 帐号：scdxt
     * 密码：dxt12345678
     * **/
    //
    public static final String URL ="http://www.wp114.cn/sms.aspx";
    //文档中就不是标准驼峰
    //企业id
    public static final String USER_ID ="1553";
    //发送用户帐号
    public static final String ACCOUNT ="scdxt";
    //发送帐号密码
    public static final String PASSWORD = "dxt12345678";
    //通用的URL前缀
    public static final String SMS_URL = URL +"?"+ "&userid=" + USER_ID + "&account=" + ACCOUNT + "&password=" +PASSWORD;
    //发送接口
    public static final String ACTION_SEND= "send";
    //余额及已发送量查询接口
    public static final String ACTION_OVERAGE= "overage";
    //非法关键词查询
    public static final String ACTION_CHECK_KEY_WORD= "checkkeyword";
    //状态报告接口 状态报告接口 和 上行接口 的 action 是一样的
    public static final String ACTION_QUERY= "query";
    //上行接口
    public static final String ACTION_UP= "query";
    //密码修改接口
    public static final String ACTION_CHANGE_PWD= "changespwd";
    //佳诺返回成功状态
    public static final String SEND_SUCCESS= "Success";


    public static void main(String[] args) throws Exception {

        //1 发送接口

        //全部被叫号码  发信发送的目的号码.多个号码之间用半角逗号隔开
        String mobile ="17381925529";
        //yingfubing
//        String mobile ="13558708865";
        // 短信的内容，内容需要UTF-8编码
        String contentInit ="【签名】中文";
        String content = URLEncoder.encode(contentInit, "UTF-8");

        //为空表示立即发送，定时发送格式 2010-10-24 09:08:10
        String sendTime ="";
        String extno = "";

        String sendUrl = new StringBuilder().append(SMS_URL).append("&action=").append(ACTION_SEND)
                .append("&mobile=").append(mobile).append("&content=").append(content)
                .append("&sendTime=").append(sendTime).append("&extno=").append(extno)
                .toString();
        String sendResultXml = HttpUtils.sendPost(sendUrl);
        System.out.println(sendResultXml);
        Map<String, String> sendResultMap = xmlToMap(sendResultXml);
        System.out.println(sendResultMap);
        String returnstatus = sendResultMap.get("returnstatus");
        System.out.println(returnstatus);
        if(SEND_SUCCESS.equals(returnstatus)){
            //业务逻辑
        }

//        //2 余额及已发送量查询接口
//        String overageUrl = new StringBuilder().append(SMS_URL).append("&action=").append(ACTION_OVERAGE).toString();
//        String overageXml = HttpUtils.sendPost(overageUrl);
//        System.out.println(overageXml);
//        Map<String, String> overageMap = xmlToMap(overageXml);
//        System.out.println(overageMap);
//        String overage = overageMap.get("overage");
//        System.out.println(overage);
//

//        //3 非法关键词查询接口
//        String keyWord = "haha";
//        String checkkeywordUrl = new StringBuilder().append(SMS_URL).append("&action=").append(ACTION_OVERAGE)
//                .append("&content=").append(keyWord)
//                .toString();
//        String checkkeywordResult = HttpUtils.sendPost(checkkeywordUrl);


     /**
      * 非法关键字
      * 华达微贷,PPGAME,无忧夺宝,日立建机(上海),恶意,催款,金宝贷,闪速钱包,装修时刻网,【合和年】尊敬,企信通66785988,【原墨装修公司】,【星敏体育】优惠促销,【妮妮箱包】,【名衣汇】,【蔓莎童装店】,【百事超市】,成都移动提速降费,诚招全国各地代理,业办理：注册公司、,足浴,中国国际货代协会.部公路院检测中心,一带一路,【小龙西瓜代办】,【雀友麻将机】,承接个人企业搬迁,起重吊装,空调移机,认准娃哈哈！买水送饮水机！详情店内咨询！,15072434112,【天虎云商】2316235回T退订,【天虎云商】啊三嘎回T退订,【天虎云商】水电费回T退订,【天虎云商】测试01回T退订,暑期学方法,引爆学习力,富力天禧城二期新品,【鸣笙房产】,【商行迎宾支行】,0元—1200.00元,现在只需598.00元。（机油、机油格、除菌和工时费
      * */

    }


}
